package com.example.falvback.controller;

import cn.hutool.core.util.StrUtil;
import com.example.falvback.common.AuthAccess;
import com.example.falvback.common.Result;
import com.example.falvback.entity.User;
import com.example.falvback.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 功能：
 * 作者：chen
 * 日期：2023/10/2410:12
 */

//@CrossOrigin//允许跨域
@RestController //相当于@Controller+@ResponseBody
public class LoginController {
    @Autowired
    LoginService loginService;
    @GetMapping("/")
    public Result hello() {
        return Result.success("hello");
    }
    @PostMapping("/login")
    public Result login(@RequestBody User user ) {
        if(StrUtil.isBlank(user.getUsername())||StrUtil.isBlank(user.getPassword())){
            return Result.error("用户名或密码不能为空");
        }

            user = loginService.login(user);//从service层中传出的是dbuser对象，在此赋值给user对象
            return Result.success(user);

    }
    @AuthAccess
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        if (StrUtil.isBlank(user.getUsername()) || StrUtil.isBlank(user.getPassword())) {
            return Result.error("数据输入不合法");
        }
        if (user.getUsername().length() > 10 || user.getPassword().length() > 20) {
            return Result.error("数据输入不合法");
        }
        user = loginService.register(user);
        return Result.success(user);
    }






}
